﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;

namespace SQLToolbox.View.UserControls
{
	/// <summary>
	/// Interaction logic for CircularProgressBar.xaml
	/// </summary>
	public partial class CircularProgressBar : UserControl
	{
		List<Brush> brushes = new List<Brush>();
		List<Ellipse> ellipses = new List<Ellipse>();
		int step = 0;
		public CircularProgressBar()
		{
			InitializeComponent();

			brushes.Add(new SolidColorBrush(Color.FromRgb(0x55, 0x54, 0x52)));
			brushes.Add(new SolidColorBrush(Color.FromRgb(0x65, 0x64, 0x61)));
			brushes.Add(new SolidColorBrush(Color.FromRgb(0x74, 0x72, 0x70)));
			brushes.Add(new SolidColorBrush(Color.FromRgb(0x83, 0x82, 0x80)));
			brushes.Add(new SolidColorBrush(Color.FromRgb(0x99, 0x97, 0x96)));
			brushes.Add(new SolidColorBrush(Color.FromRgb(0xAA, 0xA9, 0xA8)));
			brushes.Add(new SolidColorBrush(Color.FromRgb(0xB7, 0xB6, 0xB4)));
			brushes.Add(new SolidColorBrush(Color.FromRgb(0xC0, 0xBF, 0xBC)));

			ellipses.Add(e1);
			ellipses.Add(e2);
			ellipses.Add(e3);
			ellipses.Add(e4);
			ellipses.Add(e5);
			ellipses.Add(e6);
			ellipses.Add(e7);
			ellipses.Add(e8);
			for (int i = 0; i < ellipses.Count; i++)
			{
				ellipses[i].Fill = brushes[i];
			}
			DispatcherTimer timer = new DispatcherTimer();
			timer.Interval = TimeSpan.FromSeconds(0.1);
			timer.Tick += (sender, e) =>
				{
					for (int i = 0; i < ellipses.Count; i++)
					{
						ellipses[i].Fill = brushes[(i + step) % 8];
					}

					step = (++step) % 8;
				};
			timer.Start();
			this.IsVisibleChanged += (sender, e) =>
				{
					if ((!this.IsVisible) && (timer.IsEnabled))
						timer.Stop();
					if ((this.IsVisible) && (!timer.IsEnabled))
						timer.Start();
					
				};
		}


	}
}
